package org.zhixiang.gisQuery.query;

import java.util.ArrayList;
import java.util.List;

import org.zhixiang.gisQuery.rtree.Rectangle;

/**
 * 
 * @say little Boy, don't be sad.
 * @name Rezar
 * @time 2018年9月22日 上午1:17:22
 * @Desc 些年若许,不负芳华.
 *
 */
public class RectangleMbrParser {

	// public static List<RectangleMBR> parseRectangle(String polylineStr,
	// String cityCode, String cityName) {
	// List<RectangleMBR> retMbrs = new ArrayList<>();
	// String[] allPolylineStr = polylineStr.split("\\|");
	// for (String polylineItem : allPolylineStr) {
	// RectangleMBR mbr = new RectangleMBR();
	// mbr.setCityCode(cityCode);
	// mbr.setCity(cityName);
	// List<SelfPoint> locPointes = new ArrayList<>();
	// String[] split = polylineItem.split(";");
	// for (String poiItem : split) {
	// String[] split2 = poiItem.split(",");
	// locPointes.add(new SelfPoint(Double.parseDouble(split2[0]),
	// Double.parseDouble(split2[1])));
	// }
	// mbr.setPolyline(locPointes);
	// LinkedList<SelfPoint> findSmallestPolygon =
	// GisMinimumBoundingPolygon.findSmallestPolygon(locPointes);
	// Point2D.Double[] minimumBoundingRectangle = RotatingCalipers
	// .getMinimumBoundingRectangle(findSmallestPolygon);
	// java.awt.geom.Point2D.Double firstPoint = minimumBoundingRectangle[0];
	// java.awt.geom.Point2D.Double secondPoint = minimumBoundingRectangle[2];
	// List<SelfPoint> miniBound = new ArrayList<>();
	// for (Point2D.Double loc : minimumBoundingRectangle) {
	// miniBound.add(new SelfPoint(loc.x, loc.y));
	// }
	// mbr.setMiniBound(miniBound);
	// Rectangle re = new Rectangle(firstPoint.x, firstPoint.y, secondPoint.x,
	// secondPoint.y);
	// mbr.setRectangle(re);
	// retMbrs.add(mbr);
	// }
	// return retMbrs;
	// }

	public static List<RectangleMBR> parseRectangle(String polylineStr, String cityCode, String cityName) {
		List<RectangleMBR> retMbrs = new ArrayList<>();
		String[] allPolylineStr = polylineStr.split("\\|");
		for (String polylineItem : allPolylineStr) {
			RectangleMBR mbr = new RectangleMBR();
			mbr.setCityCode(cityCode);
			mbr.setCity(cityName);
			List<SelfPoint> locPointes = new ArrayList<>();
			String[] split = polylineItem.split(";");
			double minX = Double.MAX_VALUE;
			double maxX = Double.MIN_VALUE;
			double minY = Double.MAX_VALUE;
			double maxY = Double.MIN_VALUE;
			for (String poiItem : split) {
				String[] split2 = poiItem.split(",");
				SelfPoint selfPoint = new SelfPoint(Double.parseDouble(split2[0]), Double.parseDouble(split2[1]));
				locPointes.add(selfPoint);
				if (selfPoint.x < minX) {
					minX = selfPoint.x;
				}
				if (selfPoint.x > maxX) {
					maxX = selfPoint.x;
				}
				if (selfPoint.y < minY) {
					minY = selfPoint.y;
				}
				if (selfPoint.y > maxY) {
					maxY = selfPoint.y;
				}
			}
			mbr.setPolylineStr(polylineStr);
			mbr.setPolyline(locPointes);
			Rectangle re = new Rectangle(minX, minY, maxX, maxY);
			mbr.setRectangle(re);
			mbr.initMinBound(150, 150);
			retMbrs.add(mbr);
		}
		return retMbrs;
	}

}
